#include <stdio.h>
#include <stdlib.h>
//宏定义MAXSIZE
#define MAXSIZE 10
//重命名
typedef int ElemType;
typedef struct SeqList{
	ElemType * elem;
	int length;
	int size;
}SeqList,*ListPtr;
//函数原型
void InitSeqList(SeqList * list);
int ListLocate(SeqList list,ElemType e);
int ListInsert(SeqList * list,ElemType e,int pos);
int ListDelete(SeqList * list,int pos);
void ListDisplay(SeqList * list){
	for(int i=0;i<list->length;i++){
		printf("%d",list->elem[i]);
	}
	printf("\n");
}

int main(){
	SeqList list;
	InitSeqList(&list);
	for(int i=0;i<list.size;i++){
		list.elem[i]=i;
		list.length++;
	}
	//打印初始化后顺序表中的元素
	printf("原始的序列:");
	ListDisplay(&list);
	
	//查值查找
	int pos;
	ElemType e=6;
	pos=ListLocate(list,e);
	printf("定位元素的位置:");
	printf("%d\n",pos);
	
	//删除线性表中的元素并打印删除后线性表的元素
	ListDelete(&list,pos);
	printf("删除后的序列:");
	ListDisplay(&list);
	
	//向线性表中添加元素并打印
	ListInsert(&list,e,pos);
	printf("添加元素后的序列:");
	ListDisplay(&list);
	
	return 0;
}

//函数定义
void InitSeqList(SeqList * list){
	list->elem=(ElemType*)malloc(list->size*sizeof(ElemType));
	if(list->elem==NULL) 
		exit(0);
	list->length=0;
	list->size=MAXSIZE;
}

int ListLocate(SeqList list,ElemType e){
	for(int i=0;i<list.length;i++){
		if(list.elem[i]==e) 
			return (i+1);
	}
	return -1;
}

int ListInsert(SeqList * list,ElemType e,int pos){
	if((pos<1)||(pos>list->size+2)) 
		return -1;
	else if(list->length==list->size){
		list->elem=(ElemType*)realloc(list->elem,(list->size+1)*sizeof(ElemType));
		if(list->elem==NULL)
			return -1;
		list->size++;
	}
	for(int i=list->length-1;i>=pos-1;i--){
		list->elem[i+1]=list->elem[i];
	}
	list->elem[pos-1]=e;
	list->length++;
	return 0;
}

int ListDelete(SeqList * list,int pos){
	if(pos<1||pos>=list->length)
		return -1;
	for(int i=pos-1;i<list->length-1;i++){
		list->elem[i]=list->elem[i+1];
	}
	list->length--;
	return 0;
}